package com.cpit.qualitycontrol.web.controller;

import com.cpit.platform.common.entity.Result;
import com.cpit.platform.common.utils.ResultUtil;
import com.cpit.qualitycontrol.entity.UserToProductLine;
import com.cpit.qualitycontrol.service.UserToProductLineService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yanyq on 2018/4/27.
 */
@Controller
@RequestMapping(value = "/user")
public class UserToProductLineController {
    @Autowired
    private UserToProductLineService userToProductLineService;

    @ApiOperation(value = "创建用户-产品线关联")
    @ApiImplicitParam(name = "userToProductLine", value = "用户-产品线关联实例", required = true, dataType = "UserToProductLine")
    @ResponseBody
    @PreAuthorize("hasAuthority('createUser')")
    @RequestMapping(value = {"/addUserToProductLine"}, method = RequestMethod.POST)
    public Result<UserToProductLine> addUserToProductLine(@RequestBody UserToProductLine userToProductLine) {
        return ResultUtil.<UserToProductLine>success(userToProductLineService.addUserToProductLine(userToProductLine));
    }

    @ApiOperation(value = "更新用户-产品线关联")
    @ApiImplicitParam(name = "userToProductLine", value = "用户-产品线关联实例", required = true, dataType = "UserToProductLine")
    @ResponseBody
    @PreAuthorize("hasAuthority('modifyUser')")
    @RequestMapping(value = {"/updateUserToProductLine"}, method = RequestMethod.POST)
    public Result<UserToProductLine> updateUserToProductLine(@RequestBody UserToProductLine userToProductLine) {
        userToProductLineService.deleteUserToProductLine(userToProductLine.getUserId());
        return ResultUtil.<UserToProductLine>success(userToProductLineService.addUserToProductLine(userToProductLine));
    }
    @ApiOperation(value = "删除用户-产品线关联")
    @ResponseBody
    @PreAuthorize("hasAuthority('deleteUser')")
    @RequestMapping(value = {"/deleteUserToProductLine/{userid}"}, method = RequestMethod.DELETE)
    public Result<Object> deleteUserToProductLine(
            @ApiParam(value = "用户标识，此处使用内部用ID标识，并非用户的登录名。", required = true) @PathVariable("userid") Integer userid) {
        List<UserToProductLine> list = userToProductLineService.findByUserId(userid);
        if (list != null && list.size() > 0) {
            userToProductLineService.deleteUserToProductLine(userid);
        }
        return ResultUtil.<Object>success(null);
    }

    @PreAuthorize("hasAuthority('queryUser')")
    @ApiOperation(value="查询用户-产品线关联")
    @ResponseBody
    @RequestMapping(value = { "/queryUserToProductLineByUserId/{userid}" }, method = RequestMethod.GET)
    public List<UserToProductLine> queryUserToProductLineByUserId(
            @ApiParam(value = "用户ID。", required = true) @PathVariable("userid") Integer userid) {
        return userToProductLineService.findByUserId(userid);
    }
}
